home *** CD-ROM | disk | FTP | other *** search
- Path: news.microsoft.com!news
- From: a-cnadc@microsoft.com (Dann Corbit)
- Newsgroups: comp.lang.c
- Subject: Re: C unions
- Date: 6 Mar 1996 21:47:59 GMT
- Organization: Microsoft Corporation
- Message-ID: <4hl16f$8al@news.microsoft.com>
- References: <367cc$0359.14a@news.express.ca>
- NNTP-Posting-Host: 157.57.171.202
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <367cc$0359.14a@news.express.ca>, gchan@express.ca says...
- >
- >I need a C union that allows me to freely interpret an integer
- >separately as the least significant byte or the most significant byte,
- >and as a single integer.
- >
- >Can someone help me get started on this?
- >
- This is definitely dependent on the system used. ASSUMING two byte
- short, it could look like this:
-
- union dirty_trick {
- short sNumber;
- char cHiLo[2];
- };
- Which byte will be the most significant depends on 'endianness' of
- your machine, implementation of integer, etc.
- And if sNumber can be negative... yuck. Non-portable & ugly to boot.
-
- Why not just {assuming sizeof(sNumber) == 2} :
- hi = sNumber/(UCHAR_MAX+1) {don't shift unless you know it is always positive}
- lo = sNumber % (UCHAR_MAX+1)
- --
- The opinions expressed in this message are my own personal views
- and do not reflect the official views of Microsoft Corporation.
- In fact, I'm just a subcontractor, not an employee, so pull in your claws.
-
-